//
//  DHDoctorDetailsViewController.m
//  DHKuaiYi
//
//  Created by  chenhanmao on 2016/11/15.
//  Copyright © 2016年 30-陈辉华. All rights reserved.
//

#import "DHDoctorDetailsViewController.h"
#import <Masonry.h>
#import "DHGategoryView.h"
#import "XHHAdditions.h"
#import <UIImageView+WebCache.h>
#import <SVProgressHUD.h>

#define CategoryViewHeight 44
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface DHDoctorDetailsViewController ()<DHCategoryViewDelegate,UIScrollViewDelegate>

/**   中间分类视图  */
@property(nonatomic,weak) DHGategoryView *categoryView;

/**  内容视图  */
@property(nonatomic,weak) UIScrollView *contactView;

/**  头像  */
@property(nonatomic,weak) UIImageView *iconView;
/**  医生名字  */
@property(nonatomic,weak) UILabel *nameLabel;
/**  医生类型  */
@property(nonatomic,weak) UILabel *subjectLabel;
/**  医院名字  */
@property(nonatomic,weak) UILabel *hospitalLabel;
/**  预约数量  */
@property(nonatomic,weak) UIButton *orderNumberBtn;
/**  鲜花数量  */
@property(nonatomic,weak) UIButton *flowerNumberBtn;
/**  锦旗数量  */
@property(nonatomic,weak) UIButton *pennantNumberBtn;

/**   是否关注  */
@property(nonatomic,assign) BOOL isAttention;

@end


@implementation DHDoctorDetailsViewController

#pragma mark - 设置导航栏
- (void)xhhSetupNavigationItems {
    [super xhhSetupNavigationItems];
    self.title =@"医生详情";
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"saveTemplateEmpty"] style:UIBarButtonItemStylePlain target:self action:@selector(attention)];
    
//    saveTemplate

    
    self.navigationItem.rightBarButtonItem = rightBtn;
}

- (void)attention {
    
    if (!self.isAttention) {
        
        self.isAttention = !self.isAttention;
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"saveTemplate"] style:UIBarButtonItemStylePlain target:self action:@selector(attention)];
        self.navigationItem.rightBarButtonItem = rightBtn;
        [SVProgressHUD setBackgroundColor:[UIColor globalColor]];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD showSuccessWithStatus:@"关注成功!"];
        delay(1.0);
    }else{
        self.isAttention = !self.isAttention;
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"saveTemplateEmpty"] style:UIBarButtonItemStylePlain target:self action:@selector(attention)];
        self.navigationItem.rightBarButtonItem = rightBtn;
        [SVProgressHUD setBackgroundColor:[UIColor globalColor]];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD showSuccessWithStatus:@"取消关注成功!"];
        delay(1.0);
    }

}
#pragma mark - 界面布局
- (void)xhhSetupSubviews {
    //头像
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"doctor_defaultphoto_female"]];
    self.iconView = iconView;
    iconView.clipsToBounds = YES;
    [iconView.layer setCornerRadius:30];
    [self.view addSubview:iconView];
    //名字
    UILabel *nameLabel = [[UILabel alloc] init];
    self.nameLabel = nameLabel;
    nameLabel.text = @"陈辉华";
    nameLabel.font = [UIFont systemFontOfSize:28];
    [self.view addSubview:nameLabel];
    //科目
    UILabel *subjectLabel = [[UILabel alloc] init];
    self.subjectLabel = subjectLabel;
    subjectLabel.text = @"院长";
    [self.view addSubview:subjectLabel];
    //医院
    UILabel *hospitalLabel = [[UILabel alloc] init];
    self.hospitalLabel = hospitalLabel;
    hospitalLabel.text = @"黑马精神病院";
    hospitalLabel.textColor = [UIColor XHH_colorWithHex:0x838383];
    [self.view addSubview:hospitalLabel];
    //收藏、锦旗图标
    UIImageView *pennantIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flags1"]];
    [self.view addSubview:pennantIcon];
    //匹配程度
    UILabel *accuracyLabel = [[UILabel alloc] init];
    [self.view addSubview:accuracyLabel];
    accuracyLabel.text = self.doctorInfo.accuracy;
    accuracyLabel.font = [UIFont systemFontOfSize:24];
    accuracyLabel.textColor = [UIColor whiteColor];
    
    
    //预约数
    UIButton *orderNumberBtn = [self createBtnWithTitle:[NSString stringWithFormat:@"预约数：%@",self.doctorInfo.operation_count] imageName:@"add"];
    self.orderNumberBtn = orderNumberBtn;
    [self.view addSubview:orderNumberBtn];
    //鲜花数
    UIButton *flowerNumberBtn = [self createBtnWithTitle:[NSString stringWithFormat:@"鲜花数：%@",self.doctorInfo.flower] imageName:@"flower"];
    self.flowerNumberBtn = flowerNumberBtn;
    [self.view addSubview:flowerNumberBtn];
    //锦旗数
    UIButton *pennantNumberBtn = [self createBtnWithTitle:[NSString stringWithFormat:@"锦旗数：%@",self.doctorInfo.banner] imageName:@"jinqi"];
    self.pennantNumberBtn = pennantNumberBtn;
    [self.view addSubview:pennantNumberBtn];
    
    
    
    /*------------------功能内容视图(自定义)------------------*/
    
    DHGategoryView *categoryView = [[DHGategoryView alloc] init];
    
    categoryView.backgroundColor = [UIColor whiteColor];
    
    categoryView.delegate = self;
    
    [self.view addSubview: categoryView];
    
    _categoryView = categoryView;
    
    /*------------------底部内容视图------------------*/
    UIScrollView *contentView = [[UIScrollView alloc] init];
    
    contentView.showsVerticalScrollIndicator = NO;
    contentView.showsHorizontalScrollIndicator = NO;
    contentView.bounces = NO;
    contentView.pagingEnabled = YES;
    contentView.delegate = self;
    
    contentView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview: contentView];
    
    /*------------------自动布局------------------*/
    
    CGFloat iconView_leftmargin = 40;
    CGSize iconView_size = CGSizeMake(60, 60);
    CGFloat space = 8;
    CGFloat button_width = ([UIScreen mainScreen].bounds.size.width - 16 * 2 - 8 *2) / 3;
    CGFloat button_hight = 16;
    CGSize button_size = CGSizeMake(button_width, button_hight);
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).offset(iconView_leftmargin);
        make.top.equalTo(self.view).offset(72);
        make.size.mas_equalTo(iconView_size);
    }];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(iconView);
        make.left.equalTo(iconView.mas_right).offset(space);
    }];
    
    [subjectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.baseline.equalTo(nameLabel);
        make.left.equalTo(nameLabel.mas_right).offset(space);
    }];
    
    [hospitalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(space);
    }];
    
    [orderNumberBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(iconView);
        make.top.equalTo(iconView.mas_bottom).offset(space * 2);
        make.size.mas_equalTo(button_size);
    }];
    
    [flowerNumberBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(orderNumberBtn);
        make.left.equalTo(orderNumberBtn.mas_right).offset(space);
        make.size.mas_equalTo(button_size);
    }];
    
    [pennantNumberBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(orderNumberBtn);
        make.right.equalTo(self.view).offset(-space);
        make.size.mas_equalTo(button_size);
        
    }];
    
    [pennantIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.view).offset(-space);
        make.top.equalTo(self.view).offset(72);
        make.size.mas_equalTo(iconView_size);
    }];
    
    [accuracyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(pennantIcon);
    }];
    
    [categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.view);
        
        make.top.equalTo(orderNumberBtn.mas_bottom).offset(space);
        make.height.mas_equalTo(CategoryViewHeight);
    }];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(categoryView.mas_bottom);
    }];
    
    _contactView = contentView;
    
    //配置 询问内容的界面
    [self setupContentView];
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:_doctorInfo.doctor_portrait] placeholderImage:[UIImage imageNamed:@"doctor_defaultphoto_female"]];
    self.nameLabel.text = _doctorInfo.doctor_name;
    self.subjectLabel.text = _doctorInfo.doctor_title_name;
    self.hospitalLabel.text = _doctorInfo.doctor_hospital_name;
}

#pragma mark - 设置内容界面，创建控制器
- (void)setupContentView{
    
    NSArray *classNames = @[@"DHReceptionRequirementViewController",
                            @"DHDoctorResumeViewController",
                            @"DHOrderTimeViewController"];
    
    NSInteger index = 0;
    for (NSString *className in classNames) {
        
        Class cls = NSClassFromString(className);
        
        UIViewController *vc = [[cls alloc] init];
        
        // 断言, 使用名字转换容易出错
        NSAssert([vc isKindOfClass:UIViewController.class], @"不能被转换成控制器的名字");
        
        [self.contactView addSubview: vc.view];
        
        vc.view.frame = self.contactView.bounds;
        
        //        vc.view.backgroundColor = [UIColor whiteColor];
        
        [self addChildViewController: vc];
        
        //控制器添加完成
        [vc didMoveToParentViewController:self];
        
        //设置控制器视图的位置
        
        vc.view.xhh_x = SCREEN_WIDTH *index++;
    }
}

//布局完成后调用的方法
- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    
    _contactView.contentSize = CGSizeMake(_contactView.bounds.size.width * 3, _contactView.bounds.size.height);
}

//创建btn
- (UIButton *)createBtnWithTitle:(NSString *)title imageName:(NSString *)imageName {
    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];

    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];
    btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    [btn setTitleColor:[UIColor globalColor] forState:UIControlStateSelected];
    return btn;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - CHMGateforyView Delegate

- (void)categoryView:(DHGategoryView *)categoryView didSelectIndex:(NSInteger)index{
    
    [_contactView setContentOffset:CGPointMake(SCREEN_WIDTH * index, 0) animated:YES];
}

#pragma mark - UIScrollView Delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH + 0.5;
    
    [self.categoryView didSelectIndex:index];
}

#pragma mark - Setter 
- (void)setDoctorInfo:(DHDoctorModel *)doctorInfo {
    
    _doctorInfo = doctorInfo;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:doctorInfo.doctor_portrait]];
    self.nameLabel.text = doctorInfo.doctor_name;
    self.subjectLabel.text = doctorInfo.doctor_title_name;
    self.hospitalLabel.text = doctorInfo.doctor_hospital_name;
    
    
}
/*
 {
 "flower": 0,
 "operation_count": 0,
 "banner": 0,
 "doctor_hospital_name": "北京协和医院",
 "doctor_name": "何晔鑫",
 "doctor_portrait": "http://iosapi.itcast.cn/images/2093000003151445677716555.jpg",
 "doctor_id": 300000315,
 "doctor_title_name": "心理医生",
 "doctor_gender": 1,
 "accuracy": "98%",
 "easymob_id": "d300000315"
 }*/


@end
